package com.example.juegoenandroid;

import android.os.Bundle;
import android.os.Vibrator;
import android.app.Activity;
import android.content.Intent;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageButton;
import android.widget.Toast;

public class VentanaMaquina extends Activity {

	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_ventana_maquina);
		
		Toast.makeText(this,"maquina",Toast.LENGTH_SHORT).
		show();
		elegirResult();
		
	}

	private Bundle intent1;
	private int aleat=0;
	private int eleccion;
	
	public void elegirResult()
	{
		Thread maquina=new Thread(new Runnable() {
				
			
					@Override
					public void run() {
						final ImageButton resultado=(ImageButton) findViewById(R.id.ImagenResultado);
						intent1=getIntent().getExtras();
						eleccion=-1;
						for(int i=0;i<10;i++)
						{
							aleat=(int) (Math.random()*5+1);
							switch(aleat)
							{
							case 1:
								resultado.post(new Runnable() {
									
									@Override
									public void run() {
										resultado.setImageDrawable(getResources().getDrawable(R.drawable.piedra));
										resultado.setVisibility(1);			
										eleccion=3;
									}
								});
								
								break;
							case 2:
								resultado.post(new Runnable() {
									
									@Override
									public void run() {
										resultado.setImageDrawable(getResources().getDrawable(R.drawable.papel));
										resultado.setVisibility(1);	
										eleccion=4;
									}
								});
								break;
							case 3:
								resultado.post(new Runnable() {
									
									@Override
									public void run() {
										resultado.setImageDrawable(getResources().getDrawable(R.drawable.tijeras));
										resultado.setVisibility(1);
										eleccion=5;
									}
								});
								break;
							case 4:
								resultado.post(new Runnable() {
									
									@Override
									public void run() {
										resultado.setImageDrawable(getResources().getDrawable(R.drawable.lagarto));
										resultado.setVisibility(1);	
										eleccion=1;
									}
								});
								break;
							case 5:
								resultado.post(new Runnable() {
									
									@Override
									public void run() {
										resultado.setImageDrawable(getResources().getDrawable(R.drawable.im_spock));
										resultado.setVisibility(1);
										eleccion=2;
									}
								});
								break;
							}
							
							try {
								Thread.sleep(500);
							} catch (InterruptedException e) {
								e.printStackTrace();
							}
						}
						
						Intent intent=new Intent(VentanaMaquina.this,Resultado.class);
						intent.putExtra(getResources().getString(R.string.eleccion2), eleccion );
						intent.putExtra(getResources().getString(R.string.eleccion1), intent1.getInt("eleccion1"));
						intent.putExtra("nombre1", intent1.getString("nombre1"));
						intent.putExtra("nombre2", intent1.getString("nombre2"));
						intent.putExtra("modo", intent1.getBoolean("modo"));
						startActivity(intent);
						
					}
				});
				
		
		maquina.start();
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.ventana_maquina, menu);
		return true;
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			Toast.makeText(this,"No hagas trampas",Toast.LENGTH_SHORT).
			show();
			
			Vibrator v = (Vibrator) getSystemService(VIBRATOR_SERVICE);
			v.vibrate(5000);
		     return true;
		     }
		return super.onKeyDown(keyCode, event);
	}
	
	@Override
	protected void onRestart() {
		finish();
		super.onRestart();
	}
	
	
}
